package com.infra.micro.oss.domain.advice;

import com.infra.common.resp.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionAdvice {
    private static final Logger LOGGER = LoggerFactory.getLogger(GlobalExceptionAdvice.class);

    @ExceptionHandler(Throwable.class)
    public ResponseEntity handleThrowable(Throwable e) {
        LOGGER.error("系统异常", e);
        return buildResponseEntity(Response.error("系统错误，请稍候重试或联系管理员"));
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity handleIllegalArgumentException(IllegalArgumentException e) {
        LOGGER.error("参数校验异常", e);
        return buildResponseEntity(Response.error(e.getMessage()));
    }

    private ResponseEntity<Response> buildResponseEntity(Response response) {
        return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
    }
}
